
import 'mobile_app_model.dart';
import 'model_base.dart';

class AlarmMessageModel extends ModelBase {
    final String content;
    final List<String> paths;
    final MobileAppModel mobileApp;

    // 构造函数，通过 Map 初始化
    AlarmMessageModel({
        required super.id,
        required super.createdAt,
        required this.content,
        this.paths = const [],
        required this.mobileApp,
    });

    // 工厂构造函数，通过 JSON Map 创建 AlarmMessage 实例
    factory AlarmMessageModel.fromJson(Map<String, dynamic> map) {
        return AlarmMessageModel(
            id: map["id"] ?? -1,
            createdAt: DateTime.fromMillisecondsSinceEpoch(map["createdAt"] ?? 0),
            content: map["content"] ?? "",
            paths: map["paths"] != null && map["paths"].toString() != "[]"
                ? List<String>.from(map["paths"])
                : [],
            mobileApp: MobileAppModel.fromJson(map["mobileApp"] ?? {}),
        );
    }

    // 将 AlarmMessage 实例转换为 JSON Map
    Map<String, dynamic> toJson() {
        return {
            "id": id,
            "createdAt": createdAt.millisecondsSinceEpoch,
            "content": content,
            "paths": paths,
            "mobileApp": mobileApp.toJson(),
        };
    }

    // 静态方法，将动态数据转换为 AlarmMessage 列表
    static List<AlarmMessageModel> toList(dynamic res) {
        var data = List<Map<String, dynamic>>.from(res);
        return data.map((map) => AlarmMessageModel.fromJson(map)).toList();
    }
}
